local skel = fk.CreateSkill{
  name = "sj_dianjuren__keng",
}

Fk:loadTranslationTable{
  ["sj_dianjuren__keng"] = "吭",
  [":sj_dianjuren__keng"] = "出牌阶段限一次，你可以亮出牌堆顶三张牌，然后你可以将其中一张牌发起拼点：若你赢，你可以将另外一张亮出牌当【杀】使用，若造成伤害，你获得仅剩的亮出牌。结束阶段，你可以废除一个有牌的装备栏发动本技能。",
  ["#sj_dianjuren__keng"] = "吭：你可以亮出牌堆顶三张牌，然后你可以将其中一张牌发起拼点：若你赢，你可以将另外一张亮出牌当【杀】使用",
  ["#sj_dianjuren__keng_slash"] = "吭：你可以将一张亮出牌当【杀】使用",
  ["#sj_dianjuren__keng_equip"] = "吭：你可以废除一个有牌的装备栏发动〖吭〗",
}

---@param player ServerPlayer 
local keng_use = function (player)
  local room = player.room ---@cast room Room
  local cards = room:getNCards(3)
  room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, skel.name, nil, true, player)
  local ts = table.filter(room:getOtherPlayers(player),function (t)
    return player:canPindian(t,true)
  end)
  if #ts > 0 then
    local tos,ids,success = room:askToChooseCardsAndPlayers(player,{
      min_card_num = 1,
      max_card_num = 1,
      min_num = 1,
      max_num = 1,
      targets = ts,
      skill_name = skel.name,
      cancelable = true,
      expand_pile = cards,
      equal = true,
      pattern = tostring(Exppattern{id = cards}),
      prompt = "#sj_dianjuren__keng",
    })
    if success and #tos > 0 and #ids > 0 then
      local target = tos[1]
      local card = Fk:getCardById(table.remove(cards,table.indexOf(cards,ids[1])))
      local pindian = player:pindian(tos,skel.name,card)
      if pindian.results[target].winner == player then
        local use = room:askToUseVirtualCard(player,{
          name = "slash",
          skill_name = skel.name,
          prompt = "#sj_dianjuren__keng_slash",
          cancelable = true,
          skip = true,
          card_filter = {
            n = 1,
            cards = cards,
          },
        })
        if use then
          table.remove(cards,table.indexOf(cards,use.card.subcards[1]))
          room:useCard(use)
          if #room.logic:getEventsByRule(GameEvent.Damage,1,function (e)
            local damage = e.data
            return damage.card and damage.card.skillName == skel.name
          end) > 0 then
            cards = table.filter(cards, function (id)
              return room:getCardArea(id) == Card.Processing
            end)
            if #cards > 0 then
              room:obtainCard(player,cards,true,fk.ReasonJustMove,player,skel.name)
            end
          end
        end
      end
    end
  end
  cards = table.filter(cards, function (id)
    return room:getCardArea(id) == Card.Processing
  end)
  if #cards > 0 then
    room:moveCards({
      ids = cards,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonPutIntoDiscardPile,
    })
  end
end

skel:addEffect("active", {
  prompt = "#sj_dianjuren__keng",
  target_filter = Util.FalseFunc,
  card_filter = Util.FalseFunc,
  can_use = function (self, player)
    return player:usedSkillTimes(skel.name,Player.HistoryPhase) == 0
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    keng_use(player)
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.phase == Player.Finish and #player:getCardIds("e") > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local all_choices = {
      "WeaponSlot",
      "ArmorSlot",
      "DefensiveRideSlot",
      "OffensiveRideSlot",
      "TreasureSlot"
    }
    local subtypes = {
      Card.SubtypeWeapon,
      Card.SubtypeArmor,
      Card.SubtypeDefensiveRide,
      Card.SubtypeOffensiveRide,
      Card.SubtypeTreasure
    }
    local choices = {}
    for i = 1, 5, 1 do
      if #player:getAvailableEquipSlots(subtypes[i]) > 0 and #player:getEquipments(subtypes[i]) > 0 then
        table.insert(choices, all_choices[i])
      end
    end
    if #choices > 0 then
      table.insert(all_choices,"Cancel")
      table.insert(choices,"Cancel")
      local choice = room:askToChoice(player,{
        choices = choices,
        skill_name = skel.name,
        prompt = "#sj_dianjuren__keng_equip",
        all_choices = all_choices,
      })
      if choice ~= "Cancel" then
        event:setCostData(self,{choice = choice})
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:abortPlayerArea(player,event:getCostData(self).choice)
    keng_use(player)
  end,
})

return skel